﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Module_SoftController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index(string actionId)
        {
            this.ValidateRequest = false;
            ViewData["id"] = actionId;
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回软件列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Module_SoftEntity> Module_SoftSv = ServiceBuilder.BuildModule_SoftService();
            List<Module_SoftEntity> module_softList = Module_SoftSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(module_softList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看软件 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Module_SoftEntity> Module_SoftSv = ServiceBuilder.BuildModule_SoftService();
            int intId=Utils.StringToInt(actionId,0);
            Module_SoftEntity module_soft = Module_SoftSv.Look(intId);
            if (module_soft != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(module_soft), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新软件
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Module_SoftEntity module_soft = new Module_SoftEntity();
                module_soft.DataTable_Action = DataTableAction.Update;
                module_soft.Id = intId;
                module_soft.ChannelId = (Int32)Utils.GetQueryAndFormString("ChannelId", this, 10,  1 , DataType.Int);
                module_soft.Path = (String)Utils.GetQueryAndFormString("Path", this, 10,  1 , DataType.CharAndNum);
                module_soft.Title = (String)Utils.GetQueryAndFormString("Title", this, 150,  1 , DataType.Str);
                module_soft.TColor = (String)Utils.GetQueryAndFormString("TColor", this, 50,  0, DataType.Str);
                module_soft.AddDate = (DateTime)Utils.GetQueryAndFormString("AddDate", this, 23,  1 , DataType.Dat);
                module_soft.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 2147483647,  1 , DataType.Str);
                module_soft.Editor = (String)Utils.GetQueryAndFormString("Editor", this, 20,  1 , DataType.Str);
                module_soft.Author = (String)Utils.GetQueryAndFormString("Author", this, 30,  0, DataType.Str);
                module_soft.Tags = (String)Utils.GetQueryAndFormString("Tags", this, 60,  0, DataType.Str);
                module_soft.ViewNum = (Int32)Utils.GetQueryAndFormString("ViewNum", this, 10,  1 , DataType.Int);
                module_soft.IsPass =  (Int32)Utils.GetQueryAndFormString("IsPass", this, 2, 1, DataType.Int);;
                module_soft.IsImg = (bool)Utils.GetQueryAndFormString("IsImg", this, 1,  1 , DataType.Byte);
                module_soft.Img = (String)Utils.GetQueryAndFormString("Img", this, 150,  1 , DataType.Str);
                module_soft.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  1 , DataType.Int);
                module_soft.UserId = (Int32)Utils.GetQueryAndFormString("UserId", this, 10,  1 , DataType.Int);
                module_soft.ReadGroup = (String)Utils.GetQueryAndFormString("ReadGroup", this, 50,  1 , DataType.Str);
                module_soft.SourceFrom = (String)Utils.GetQueryAndFormString("SourceFrom", this, 30,  0, DataType.Str);
                module_soft.Version = (String)Utils.GetQueryAndFormString("Version", this, 100,  1 , DataType.Str);
                module_soft.OperatingSystem = (String)Utils.GetQueryAndFormString("OperatingSystem", this, 255,  0, DataType.Str);
                module_soft.UnZipPass = (String)Utils.GetQueryAndFormString("UnZipPass", this, 100,  1 , DataType.Str);
                module_soft.DemoUrl = (String)Utils.GetQueryAndFormString("DemoUrl", this, 255,  0, DataType.Str);
                module_soft.RegUrl = (String)Utils.GetQueryAndFormString("RegUrl", this, 255,  0, DataType.Str);
                module_soft.SSize = (String)Utils.GetQueryAndFormString("SSize", this, 20,  0, DataType.Str);
                module_soft.Points = (Int32)Utils.GetQueryAndFormString("Points", this, 10,  1 , DataType.Int);
                module_soft.DownUrl = (String)Utils.GetQueryAndFormString("DownUrl", this, 2147483647,  1 , DataType.Str);
                module_soft.DownNum = (Int32)Utils.GetQueryAndFormString("DownNum", this, 10,  1 , DataType.Int);
                module_soft.OutUrl = (String)Utils.GetQueryAndFormString("OutUrl", this, 100,  0, DataType.Str);
                module_soft.ReadComment = (String)Utils.GetQueryAndFormString("ReadComment", this, 50,  1 , DataType.Str);
                module_soft.WriteComment = (String)Utils.GetQueryAndFormString("WriteComment", this, 50,  1 , DataType.Str);
                module_soft.AgainstNum = (Int32)Utils.GetQueryAndFormString("AgainstNum", this, 10,  1 , DataType.Int);
                module_soft.SupportNum = (Int32)Utils.GetQueryAndFormString("SupportNum", this, 10,  1 , DataType.Int);
                module_soft.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 1,  1 , DataType.Byte);
                IDataService<Module_SoftEntity> Module_SoftSv = ServiceBuilder.BuildModule_SoftService();
                if (Module_SoftSv.Update(module_soft) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增软件
        /// </summary>
        /// <returns>json</returns>
        public string Insert(string actionId)
        {
            int channelId = 0;
            channelId = Utils.StringToInt(actionId.ToString(), 0);
            Module_SoftEntity module_soft = new Module_SoftEntity();
            module_soft.DataTable_Action = DataTableAction.Insert;
            module_soft.ChannelId = channelId;
            module_soft.Path = "";
            module_soft.Title = (String)Utils.GetQueryAndFormString("Title", this, 150,  1 , DataType.Str);
            module_soft.TColor = (String)Utils.GetQueryAndFormString("TColor", this, 50,  0, DataType.Str);
            module_soft.AddDate = (DateTime)Utils.GetQueryAndFormString("AddDate", this, 23,  1 , DataType.Dat);
            module_soft.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 2147483647,  1 , DataType.Str);
            module_soft.Editor = (String)Utils.GetQueryAndFormString("Editor", this, 20,  1 , DataType.Str);
            module_soft.Author = (String)Utils.GetQueryAndFormString("Author", this, 30,  0, DataType.Str);
            module_soft.Tags = (String)Utils.GetQueryAndFormString("Tags", this, 60,  0, DataType.Str);
            module_soft.ViewNum = (Int32)Utils.GetQueryAndFormString("ViewNum", this, 10,  1 , DataType.Int);
            module_soft.IsPass =  (Int32)Utils.GetQueryAndFormString("IsPass", this, 2, 1, DataType.Int);;
            module_soft.IsImg = (bool)Utils.GetQueryAndFormString("IsImg", this, 1,  1 , DataType.Byte);
            module_soft.Img = (String)Utils.GetQueryAndFormString("Img", this, 150,  1 , DataType.Str);
            module_soft.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  1 , DataType.Int);
            module_soft.UserId = (Int32)Utils.GetQueryAndFormString("UserId", this, 10,  1 , DataType.Int);
            module_soft.ReadGroup = (String)Utils.GetQueryAndFormString("ReadGroup", this, 50,  1 , DataType.Str);
            module_soft.SourceFrom = (String)Utils.GetQueryAndFormString("SourceFrom", this, 30,  0, DataType.Str);
            module_soft.Version = (String)Utils.GetQueryAndFormString("Version", this, 100,  1 , DataType.Str);
            module_soft.OperatingSystem = (String)Utils.GetQueryAndFormString("OperatingSystem", this, 255,  0, DataType.Str);
            module_soft.UnZipPass = (String)Utils.GetQueryAndFormString("UnZipPass", this, 100,  1 , DataType.Str);
            module_soft.DemoUrl = (String)Utils.GetQueryAndFormString("DemoUrl", this, 255,  0, DataType.Str);
            module_soft.RegUrl = (String)Utils.GetQueryAndFormString("RegUrl", this, 255,  0, DataType.Str);
            module_soft.SSize = (String)Utils.GetQueryAndFormString("SSize", this, 20,  0, DataType.Str);
            module_soft.Points = (Int32)Utils.GetQueryAndFormString("Points", this, 10,  1 , DataType.Int);
            module_soft.DownUrl = (String)Utils.GetQueryAndFormString("DownUrl", this, 2147483647,  1 , DataType.Str);
            module_soft.DownNum = (Int32)Utils.GetQueryAndFormString("DownNum", this, 10,  1 , DataType.Int);
            module_soft.OutUrl = (String)Utils.GetQueryAndFormString("OutUrl", this, 100,  0, DataType.Str);
            module_soft.ReadComment = (String)Utils.GetQueryAndFormString("ReadComment", this, 50,  1 , DataType.Str);
            module_soft.WriteComment = (String)Utils.GetQueryAndFormString("WriteComment", this, 50,  1 , DataType.Str);
            module_soft.AgainstNum = (Int32)Utils.GetQueryAndFormString("AgainstNum", this, 10,  1 , DataType.Int);
            module_soft.SupportNum = (Int32)Utils.GetQueryAndFormString("SupportNum", this, 10,  1 , DataType.Int);
            module_soft.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 1,  1 , DataType.Byte);
           IDataService<Module_SoftEntity> Module_SoftSv = ServiceBuilder.BuildModule_SoftService();
           if (Module_SoftSv.Insert(module_soft) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除软件
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Module_SoftEntity module_soft = new Module_SoftEntity();
                module_soft.Id = intId;
                IDataService<Module_SoftEntity> Module_SoftSv = ServiceBuilder.BuildModule_SoftService();
                if (Module_SoftSv.Delete(module_soft) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除软件
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Module_SoftEntity> Module_SoftSv = ServiceBuilder.BuildModule_SoftService();
                if (Module_SoftSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
